﻿using AutoMapper;
using Serilog;
using SqlSugar;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Text;
using System.Threading.Tasks;
using WCS_Entity.AutoQeuipment;
using WCS_Entity.Dto.AutoEquipmentDto;
using WCS_Entity.Dto.MapPositionDto;
using WCS_Entity.Entity;
using WCS_Repository.Interface;
using WCS_Repository.Repositories;
using WCS_Service.Interface;

namespace WCS_Service.Services
{
    /// <summary>
    /// 地图点位服务
    /// </summary>
    public class MapPositionService : BaseService<MapPositionEntity>, IMapPositionService
    {
        private readonly IMapPositionRepository _mapPositionRepository;
        private readonly IMapper _mapper;

        #region 构造
        public MapPositionService(IMapPositionRepository mapPositionRepository, IMapper mapper) 
        {
            _mapPositionRepository= mapPositionRepository;
            _mapper= mapper;
        }
        #endregion
        public async Task<MapPositionDto> CreateMapPositonAsync(CreatMapPositionDto dto)
        {
            MapPositionDto result = new MapPositionDto();
            var flag = await _mapPositionRepository.ExistsAsync(a =>
            a.mapPositionCode == dto.mapPositionCode && a.mapCode == dto.mapCode);
            if (flag)
            {
                Log.Error($"点位信息{dto.mapPositionCode}已经添加!");
                return null;
            }
            var entity = _mapper.Map<MapPositionEntity>(dto);
            entity.createTime = DateTime.Now;

            await _mapPositionRepository.InsertAsync(entity);
            result = _mapper.Map<MapPositionDto>(entity);
            return result;
        }

        public async Task<bool> DeleteMapPositon(int id)
        {
            return await _mapPositionRepository.DeleteByIdAsync(id);
        }

        public async Task<List<MapPositionDto>> QueryAllMapPositonAsync()
        {
            List<MapPositionDto> result = new List<MapPositionDto>();
            Expression<Func<MapPositionEntity, object>> orderEx = p => p.createTime;
            var entityInfos = await _mapPositionRepository.QueryPageAsync(null, orderEx, OrderByType.Desc, 1, 10);
            if (entityInfos is null || !entityInfos.Any())
            {
                Log.Error("查询所有点位信息失败");
                return null;
            }
            List<Task<MapPositionDto>> entityList = new List<Task<MapPositionDto>>();
            foreach (var entityInfo in entityInfos)
            {
                entityList.Add(GetDtoInfoAsync(entityInfo));
            }
            await Task.WhenAll(entityList);
            result = entityList.Select(e => e.Result).ToList();
            return result;
        }

        public async Task<MapPositionDto> QueryMapPositionBaseBinNumberAsync(string areaCode, string binNumber)
        {

            MapPositionDto result = new();
            Expression<Func<MapPositionEntity, bool>> expression = eq => eq.binNumber == binNumber
                                                                     && eq.areaCode == areaCode;
            var entitys = await _mapPositionRepository.QueryListByClauseAsync(expression);
            if (entitys is null || !entitys.Any())
            {
                Log.Error($"查询仓位{binNumber}的地图点位信息不存在");
                return null;
            }
            var entity = entitys.FirstOrDefault();
            if (entity is null)
            {
                Log.Error($"查询仓位{binNumber}的地图点位信息不存在");
                return null;
            }
            result = _mapper.Map<MapPositionDto>(entity);
            return result;
        }

        public async Task<MapPositionDto> QueryMapPositionBasePortNumberAsync(string areaCode, string binNumber, string port)
        {
            MapPositionDto result = new();
            Expression<Func<MapPositionEntity, bool>> expression = eq => eq.binNumber == binNumber
                                                                        && eq.areaCode == areaCode
                                                                        && eq.portNumber == port;
            var entitys = await _mapPositionRepository.QueryListByClauseAsync(expression);
            if (entitys is null || !entitys.Any())
            {
                Log.Error($"查询仓位{binNumber}的地图点位信息不存在");
                return null;
            }
            var entity = entitys.FirstOrDefault();
            if (entity is null)
            {
                Log.Error($"查询仓位{binNumber}的地图点位信息不存在");
                return null;
            }
            result = _mapper.Map<MapPositionDto>(entity);
            return result;
        }

        public async Task<MapPositionDto> QueryMapPositonAsync(int id)
        {
            MapPositionDto result = new MapPositionDto();
            var entity = await _mapPositionRepository.QueryByIdAsync(id);
            if (entity is null)
            {
                Log.Error($"查询地图点位{id}的信息不存在");
                return null;
            }
            result = _mapper.Map<MapPositionDto>(entity);
            return result;
        }

        public async Task<MapPositionDto> UpdateMapPositontAsync(UpdateMapPositionDto dto)
        {
            MapPositionDto result = new MapPositionDto();
            var entity = await _mapPositionRepository.QueryByIdAsync(dto.id);
            if (entity is null)
            {
                Log.Error($"更新点位信息{dto.id}没有查询到!");
                return null;
            }
            if (entity.mapPositionCode != dto.mapPositionCode)
            {
                Log.Error($"更新地图点位{dto.mapPositionCode}的编号不一致!");
                return null;
            }
            if (entity.areaCode != dto.areaCode)
            { 
                entity.areaCode = dto.areaCode;
            }
            if (entity.binNumber != dto.binNumber)
            { 
                entity.binNumber = dto.binNumber;
            }
            if (entity.portNumber != dto.portNumber)
            { 
                entity.portNumber = dto.portNumber;
            }
            if (entity.mapCode != dto.mapCode)
            { 
                entity.mapCode = dto.mapCode;
            }
            if (entity.mapPositionCode != dto.mapPositionCode)
            { 
                entity.mapPositionCode = dto.mapPositionCode;
            }
            if (entity.Extend1 != dto.Extend1)
            {
                entity.Extend1 = dto.Extend1;
            }
            if (entity.Extend2 != dto.Extend2)
            {
                entity.Extend2 = dto.Extend2;
            }
            if (entity.Extend3 != dto.Extend3)
            {
                entity.Extend3 = dto.Extend3;
            }
            if (entity.Extend4 != dto.Extend4)
            {
                entity.Extend4 = dto.Extend4;
            }
            if (entity.Extend5 != dto.Extend5)
            {
                entity.Extend5 = dto.Extend5;
            }
            entity.updataTime = DateTime.Now;
            await _mapPositionRepository.UpdateAsync(entity);
            result = _mapper.Map<MapPositionDto>(entity);
            return result;
        }

        #region private
        private async Task<MapPositionDto> GetDtoInfoAsync(MapPositionEntity entity)
        {
            return await Task.Run(() => _mapper.Map<MapPositionDto>(entity));
        }
        #endregion
    }
}
